<template>
  <div class="public-layout">
    <!-- 顶部导航栏 -->
    <header class="public-header">
      <div class="header-left">
        <div class="logo">
          <i class="icon-logo"></i>
          <span class="logo-text">农民技能提升就业培训大数据平台</span>
        </div>
      </div>
      
      <div class="header-right">
        <router-link to="/login" class="login-button">
          <i class="icon-user"></i>
          登录
        </router-link>
      </div>
    </header>
    
    <!-- 主要内容区域 -->
    <main class="public-main">
      <div class="welcome-section" v-if="!announcements.length && !loading">
        <h1>欢迎访问农民技能提升就业培训大数据平台</h1>
        <p>请登录以访问管理功能</p>
      </div>
      
      <!-- 公告展示区域 -->
      <div class="announcements-section" v-else>
        <!-- 轮播图区域 -->
        <div class="carousel-section" v-if="topAnnouncements.length > 0">
          <div class="carousel-container">
            <div class="carousel-wrapper" :style="{ transform: `translateX(-${currentSlide * 100}%)` }">
              <div 
                v-for="(announcement, index) in topAnnouncements" 
                :key="index" 
                class="carousel-slide"
                @click="viewAnnouncementDetail(announcement)"
              >
                <div class="carousel-image">
                  <img :src="announcement.cover_image || defaultImage" :alt="announcement.title">
                  <div class="carousel-content">
                    <h3 class="carousel-title">{{ announcement.title }}</h3>
                    <p class="carousel-summary">{{ announcement.abstract }}</p>
                    <div class="carousel-meta">
                      <span class="announcement-date">{{ formatDate(announcement.created_at) }}</span>
                      <span class="announcement-status status-top">置顶</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            
            <!-- 轮播指示器 -->
            <div class="carousel-indicators">
              <span 
                v-for="(announcement, index) in topAnnouncements" 
                :key="index"
                :class="{ active: currentSlide === index }"
                @click="goToSlide(index)"
              ></span>
            </div>
            
            <!-- 轮播控制按钮 -->
            <button class="carousel-control prev" @click="prevSlide">&#10094;</button>
            <button class="carousel-control next" @click="nextSlide">&#10095;</button>
          </div>
        </div>
        
        <!-- 其他公告 -->
        <div class="other-announcements" v-if="otherAnnouncements.length > 0">
          <h2 class="section-title">更多公告</h2>
          <div class="announcements-grid">
            <div 
              v-for="(announcement, index) in paginatedOtherAnnouncements" 
              :key="announcement.id" 
              class="announcement-card"
              :class="{ 'with-image': shouldShowImage(index), 'without-image': !shouldShowImage(index) }"
              @click="viewAnnouncementDetail(announcement)"
            >
              <div v-if="shouldShowImage(index)" class="announcement-image">
                <img :src="announcement.cover_image || defaultImage" :alt="announcement.title">
              </div>
              <div class="announcement-content">
                <h3 class="announcement-title">{{ announcement.title }}</h3>
                <div class="announcement-meta">
                  <span class="announcement-date">{{ formatDate(announcement.created_at) }}</span>
                  <span 
                    class="announcement-status" 
                    :class="announcement.is_top ? 'status-top' : 'status-normal'"
                  >
                    {{ announcement.is_top ? '置顶' : '普通' }}
                  </span>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 分页 -->
          <div class="pagination" v-if="totalOtherPages > 1">
            <button 
              :disabled="currentOtherPage === 1" 
              @click="changeOtherPage(currentOtherPage - 1)"
              class="pagination-button"
            >
              上一页
            </button>
            
            <span 
              v-for="page in totalOtherPages" 
              :key="page"
              class="pagination-number"
              :class="{ active: currentOtherPage === page }"
              @click="changeOtherPage(page)"
            >
              {{ page }}
            </span>
            
            <button 
              :disabled="currentOtherPage === totalOtherPages" 
              @click="changeOtherPage(currentOtherPage + 1)"
              class="pagination-button"
            >
              下一页
            </button>
          </div>
        </div>
      </div>
      
      <!-- 公告详情模态框 -->
      <div class="modal-overlay" v-if="showDetailModal" @click="closeDetailModal">
        <div class="modal-container" @click.stop>
          <div class="modal-content">
            <div class="modal-header">
              <h2>{{ detailAnnouncement.title }}</h2>
              <span class="close" @click="closeDetailModal">&times;</span>
            </div>
            <div class="modal-body">
              <div class="announcement-detail-meta">
                <span class="announcement-date">{{ formatDate(detailAnnouncement.created_at) }}</span>
                <span 
                  class="announcement-status" 
                  :class="detailAnnouncement.is_top ? 'status-top' : 'status-normal'"
                >
                  {{ detailAnnouncement.is_top ? '置顶' : '普通' }}
                </span>
              </div>
              
              <div class="announcement-detail-image" v-if="detailAnnouncement.cover_image">
                <img :src="detailAnnouncement.cover_image" :alt="detailAnnouncement.title">
              </div>
              
              <div class="announcement-detail-content" v-html="detailAnnouncement.content"></div>
            </div>
          </div>
        </div>
      </div>
    </main>
    
    <!-- 底部footer -->
    <footer class="public-footer">
      <div class="footer-content">
        <div class="footer-section">
          <h3>联系我们</h3>
          <p>地址：北京市朝阳区农业大厦123号</p>
          <p>电话：010-12345678</p>
          <p>邮箱：contact@farmerskills.gov.cn</p>
        </div>
        
        <div class="footer-section">
          <h3>友情链接</h3>
          <ul>
            <li><a href="#">农业农村部</a></li>
            <li><a href="#">人力资源与社会保障部</a></li>
            <li><a href="#">中华全国总工会</a></li>
            <li><a href="#">中国就业培训技术指导中心</a></li>
          </ul>
        </div>
        
        <div class="footer-section">
          <h3>关注我们</h3>
          <div class="social-links">
            <a href="#" class="social-icon">微信</a>
            <a href="#" class="social-icon">微博</a>
            <a href="#" class="social-icon">抖音</a>
          </div>
        </div>
      </div>
      
      <div class="footer-bottom">
        <p>&copy; 2023 农民技能提升就业培训大数据平台. 保留所有权利。</p>
        <p>京ICP备12345678号-1 京公网安备12345678901234号</p>
      </div>
    </footer>
  </div>
</template>

<script>
import axios from 'axios';

// API基础URL - 与NewsManagement.vue中保持一致
const API_BASE_URL = 'http://localhost:8000/news';

export default {
  name: 'Public',
  data() {
    return {
      currentSlide: 0,
      slideInterval: null,
      slideIntervalTime: 5000, // 5秒切换一次
      currentOtherPage: 1,
      itemsPerPage: 9, // 3列3排
      defaultImage: 'https://via.placeholder.com/300x200?text=公告图片',
      announcements: [],
      loading: false,
      totalItems: 0,
      // 公告详情相关
      showDetailModal: false,
      detailAnnouncement: {}
    }
  },
  computed: {
    // 前三个置顶公告用于轮播
    topAnnouncements() {
      return this.announcements.filter(a => a.is_top).slice(0, 3);
    },
    
    // 其他公告
    otherAnnouncements() {
      // 排除前3个置顶公告，其他所有公告
      const topIds = this.topAnnouncements.map(a => a.id);
      return this.announcements.filter(a => !topIds.includes(a.id));
    },
    
    // 当前页的其他公告
    paginatedOtherAnnouncements() {
      const start = (this.currentOtherPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.otherAnnouncements.slice(start, end);
    },
    
    // 其他公告总页数
    totalOtherPages() {
      return Math.ceil(this.otherAnnouncements.length / this.itemsPerPage);
    }
  },
  mounted() {
    this.fetchAnnouncements();
    this.startSlideShow();
  },
  beforeUnmount() {
    this.stopSlideShow();
  },
  methods: {
    // 从后端获取公告数据
    async fetchAnnouncements() {
      this.loading = true;
      try {
        // 构造参数对象
        const params = {
          skip: 0,
          limit: 100 // 获取足够多的数据以支持分页
        };
        
        // 只有当需要筛选置顶状态时才添加is_top参数
        // 不传is_top参数表示获取所有公告
        
        const response = await axios.get(API_BASE_URL + '/', { params });
        
        // 解析响应数据
        let items = [];
        if (Array.isArray(response.data)) {
          items = response.data;
        } else if (response.data.items) {
          items = response.data.items;
        }
        
        // 按置顶和创建时间排序
        this.announcements = items.sort((a, b) => {
          // 置顶公告排在前面
          if (a.is_top && !b.is_top) return -1;
          if (!a.is_top && b.is_top) return 1;
          
          // 都是置顶或都不是置顶，按创建时间倒序排列
          return new Date(b.created_at) - new Date(a.created_at);
        });
        
        this.totalItems = this.announcements.length;
      } catch (error) {
        console.error('获取公告列表失败:', error);
      } finally {
        this.loading = false;
      }
    },
    
    // 判断是否应该显示图片（仅第一页的前几个公告显示图片）
    shouldShowImage(index) {
      return this.currentOtherPage === 1 && index < 3;
    },
    
    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      return date.toLocaleDateString('zh-CN');
    },
    
    // 切换其他公告页面
    changeOtherPage(page) {
      if (page >= 1 && page <= this.totalOtherPages) {
        this.currentOtherPage = page;
        // 滚动到页面顶部
        window.scrollTo({ top: 0, behavior: 'smooth' });
      }
    },
    
    // 查看公告详情
    viewAnnouncementDetail(announcement) {
      this.detailAnnouncement = announcement;
      this.showDetailModal = true;
      document.body.style.overflow = 'hidden'; // 防止背景滚动
    },
    
    // 关闭详情模态框
    closeDetailModal() {
      this.showDetailModal = false;
      document.body.style.overflow = ''; // 恢复背景滚动
    },
    
    // 轮播相关方法
    startSlideShow() {
      this.slideInterval = setInterval(() => {
        this.nextSlide();
      }, this.slideIntervalTime);
    },
    
    stopSlideShow() {
      if (this.slideInterval) {
        clearInterval(this.slideInterval);
        this.slideInterval = null;
      }
    },
    
    nextSlide() {
      if (this.topAnnouncements.length > 0) {
        this.currentSlide = (this.currentSlide + 1) % this.topAnnouncements.length;
      }
    },
    
    prevSlide() {
      if (this.topAnnouncements.length > 0) {
        this.currentSlide = (this.currentSlide - 1 + this.topAnnouncements.length) % this.topAnnouncements.length;
      }
    },
    
    goToSlide(index) {
      this.currentSlide = index;
    }
  }
}
</script>

<style scoped>
.public-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.public-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  background: linear-gradient(90deg, #409eff 0%, #337ecc 100%);
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.2);
  color: white;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
}

.logo .icon-logo {
  font-size: 24px;
  margin-right: 10px;
  color: white;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: white;
}

.header-right {
  display: flex;
  align-items: center;
}

.login-button {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  text-decoration: none;
  border-radius: 20px;
  transition: all 0.3s;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.login-button:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.login-button .icon-user {
  margin-right: 5px;
}

.public-main {
  padding: 40px 20px;
  flex: 1;
}

.welcome-section {
  text-align: center;
  max-width: 800px;
  margin: 0 auto;
  background: white;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.welcome-section::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(64, 158, 255, 0.1) 0%, transparent 70%);
  z-index: 0;
}

.welcome-section h1 {
  color: #333;
  margin-bottom: 20px;
  position: relative;
  z-index: 1;
}

.welcome-section p {
  color: #666;
  font-size: 16px;
  position: relative;
  z-index: 1;
}

/* 轮播图样式 */
.carousel-section {
  margin-bottom: 40px;
}

.carousel-container {
  position: relative;
  overflow: hidden;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  height: 400px;
}

.carousel-wrapper {
  display: flex;
  transition: transform 0.5s ease-in-out;
  height: 100%;
}

.carousel-slide {
  min-width: 100%;
  height: 100%;
  cursor: pointer;
}

.carousel-image {
  position: relative;
  width: 100%;
  height: 100%;
}

.carousel-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-content {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 30px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
  color: white;
}

.carousel-title {
  font-size: 24px;
  margin-bottom: 10px;
  color: white;
}

.carousel-summary {
  font-size: 16px;
  margin-bottom: 15px;
  color: #f0f0f0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.carousel-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
}

.carousel-indicators span {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: background 0.3s;
}

.carousel-indicators span.active {
  background: white;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  font-size: 24px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.3s;
}

.carousel-control.prev {
  left: 20px;
}

.carousel-control.next {
  right: 20px;
}

.carousel-control:hover {
  background: rgba(0, 0, 0, 0.8);
}

/* 其他公告样式 */
.section-title {
  font-size: 22px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #409eff;
}

.announcements-section {
  max-width: 1200px;
  margin: 0 auto;
}

.announcements-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  margin-bottom: 30px;
}

.announcement-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  transform-style: preserve-3d;
}

.announcement-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.05) 0%, transparent 100%);
  z-index: 0;
}

.announcement-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
}

.announcement-card.with-image .announcement-content {
  padding: 15px;
}

.announcement-card.without-image .announcement-content {
  padding: 15px;
}

.announcement-image {
  height: 150px;
  overflow: hidden;
  position: relative;
}

.announcement-image::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 40%;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.5), transparent);
}

.announcement-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.announcement-card:hover .announcement-image img {
  transform: scale(1.05);
}

.announcement-content {
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 1;
}

.announcement-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin: 0 0 8px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.announcement-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  padding-top: 10px;
  border-top: 1px dashed #eee;
}

.announcement-date {
  color: #999;
}

.announcement-status {
  padding: 3px 8px;
  border-radius: 12px;
  font-weight: bold;
}

.announcement-status.status-top {
  background: linear-gradient(135deg, #ff6b6b 0%, #ff8e8e 100%);
  color: white;
  box-shadow: 0 2px 5px rgba(255, 107, 107, 0.3);
}

.announcement-status.status-normal {
  background: linear-gradient(135deg, #409eff 0%, #79bbff 100%);
  color: white;
  box-shadow: 0 2px 5px rgba(64, 158, 255, 0.3);
}

/* 分页样式 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-top: 30px;
  flex-wrap: wrap;
}

.pagination-button,
.pagination-number {
  padding: 6px 12px;
  border: 1px solid #dcdfe6;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.pagination-button:hover:not(:disabled),
.pagination-number:hover,
.pagination-number.active {
  background: linear-gradient(135deg, #409eff 0%, #337ecc 100%);
  color: white;
  border-color: #409eff;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(64, 158, 255, 0.3);
}

.pagination-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
  box-shadow: none;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
}

.modal-container {
  background: white;
  border-radius: 15px;
  max-width: 800px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  animation: modalAppear 0.3s ease-out;
}

@keyframes modalAppear {
  from {
    opacity: 0;
    transform: translateY(-50px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
  background: linear-gradient(90deg, #f5f7fa 0%, #e4edf9 100%);
  border-radius: 15px 15px 0 0;
}

.modal-header h2 {
  margin: 0;
  color: #333;
  font-size: 22px;
  font-weight: bold;
}

.close {
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
  color: #aaa;
  transition: all 0.2s;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.close:hover {
  color: #333;
  background: rgba(0, 0, 0, 0.05);
}

.modal-body {
  padding: 20px;
}

.announcement-detail-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.announcement-detail-image {
  text-align: center;
  margin-bottom: 20px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.announcement-detail-image img {
  max-width: 100%;
  height: auto;
}

.announcement-detail-content {
  line-height: 1.8;
  color: #333;
  font-size: 15px;
}

/* 底部footer样式 */
.public-footer {
  background: linear-gradient(135deg, #2c3e50 0%, #1a2a3a 100%);
  color: #ecf0f1;
  padding: 30px 20px 15px;
  margin-top: auto;
  flex-shrink: 0;
}

.footer-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto 20px;
}

.footer-section h3 {
  color: #3498db;
  margin-bottom: 15px;
  font-size: 18px;
  position: relative;
  padding-bottom: 8px;
}

.footer-section h3::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 50px;
  height: 2px;
  background: #3498db;
}

.footer-section p {
  margin: 8px 0;
  font-size: 14px;
  color: #bdc3c7;
  line-height: 1.6;
}

.footer-section ul {
  list-style: none;
  padding: 0;
}

.footer-section ul li {
  margin: 10px 0;
}

.footer-section ul li a {
  color: #bdc3c7;
  text-decoration: none;
  transition: all 0.3s;
  font-size: 14px;
}

.footer-section ul li a:hover {
  color: #3498db;
  padding-left: 5px;
}

.social-links {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.social-icon {
  display: inline-block;
  padding: 8px 15px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  color: #ecf0f1;
  text-decoration: none;
  transition: all 0.3s;
  font-size: 14px;
}

.social-icon:hover {
  background: #3498db;
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 12px;
  color: #95a5a6;
}

.footer-bottom p {
  margin: 5px 0;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .announcements-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .announcement-card {
    max-height: 250px;
  }
  
  .carousel-container {
    height: 300px;
  }
  
  .carousel-title {
    font-size: 20px;
  }
}

@media (max-width: 768px) {
  .announcements-grid {
    grid-template-columns: 1fr;
  }
  
  .public-main {
    padding: 20px 10px;
  }
  
  .announcements-section {
    padding: 0 10px;
  }
  
  .modal-container {
    margin: 20px;
    max-width: calc(100% - 40px);
  }
  
  .announcement-card {
    max-height: none;
  }
  
  .footer-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .carousel-container {
    height: 250px;
  }
  
  .carousel-content {
    padding: 15px;
  }
  
  .carousel-title {
    font-size: 18px;
  }
  
  .carousel-summary {
    font-size: 14px;
  }
  
  .carousel-control {
    width: 40px;
    height: 40px;
    font-size: 20px;
  }
  
  .carousel-control.prev {
    left: 10px;
  }
  
  .carousel-control.next {
    right: 10px;
  }
}
</style>